package com.market.cash.pro

import com.market.cash.CashRootApps
import com.market.cash.base.BasePresenter
import com.market.cash.http.IHttpCallback
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/16 10:32
 * @Annotation：
 */
abstract class ProPresenter(private val iView: IProView): BasePresenter() {

    fun getString(strId: Int): String{
        return CashRootApps.app.getString(strId)
    }

    /**
     * 接口请求
     */
    fun doCashRootPostAction(httpUrl: String,params: JSONObject,callback: IHttpCallback){
        iView.getAct().doPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                       callback.onRequestSuccess(data)
                    }
                    else -> {
                        callback.onRequestError(data)
                        val msg = data.optString("msg")
                        iView.showCashRootToast(msg)
                        iView.hideCashRootLoading()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                iView.showCashRootToast(error.optString("errorMsg"))
                iView.hideCashRootLoading()
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点事件
     */
    fun addCashRootEventPoint(eventName: String, eventRemark: String = ""){
        iView.getAct().addCashRootEventPoint(eventName,eventRemark)
    }
}